package com.bear.newsset.dao.sql;

import android.os.SystemClock;
import androidx.annotation.Nullable;
import android.util.Base64;

import com.bear.newsset.dao.AccountEntityDao;
import com.bear.newsset.dao.SQLAction;
import com.bear.newsset.dao.entity.AccountEntity;

import java.util.List;

/**
 * 账号SQL
 */
public class AccountSQL {
    private AccountEntityDao mAccountDao;

    private AccountSQL() {
        SQLAction action = SQLAction.getInstance();
        mAccountDao = action.getAccountEntityDao();
    }

    public static AccountSQL getInstance() {
        return AccountSQL.HANDLER.INSTANCE;
    }

    private static final class HANDLER {
        private static final AccountSQL INSTANCE = new AccountSQL();
    }

    public AccountEntity createTestAccount() {
        AccountEntity e = new AccountEntity();
        String uuid = String.valueOf(System.currentTimeMillis());

        e.setAccount( Base64.encodeToString(uuid.getBytes(), Base64.DEFAULT) );
        e.setToken( uuid + SystemClock.currentThreadTimeMillis() );
        return e;
    }

    /**
     * 插入账号
     * @param entity    账号信息
     * @return          游标
     */
    public long insertAccount(AccountEntity entity) {
        return mAccountDao.insertOrReplace( entity );
    }

    /**
     * 获取所有账号
     * @return      账号列表
     */
    public List<AccountEntity> queryAccountAll() {
        return mAccountDao.queryBuilder().list();
    }

    /**
     * 查询第一条账号
     * @return      账号信息
     */
    @Nullable
    public AccountEntity queryAccountTop() {
        List<AccountEntity> list = queryAccountAll();
        return list != null && list.size() > 0 ? list.get( 0 ) : null;
    }
}
